<template>
	<view class="unt-selfaddress-contanier">
		<view class="user-check-address" @click="addressChangeModel()">
			<!-- color="#01ab63" -->
			<u-icon name="map" size="20"></u-icon>
			<view class="addres-content">
				<view class="contacts"><text class="name">{{address.name}}</text> <text
						class="tep">{{address.phone}}</text></view>
				<view class="addres-detailed">{{address.addressName}}</view>
			</view>
			<unt-icon name="icon-arrow" size="12" color="#01a601"></unt-icon>
		</view>
		<u-popup :show="show" @close="show=false" @open="show=true"  round='20'>
			<view class="addres-list" v-if="list.length">
				<view class="item card-shadow" v-for="(item,index) in list" :key="index">
					<view class="name-tep">
						<text class="name">{{item.name}}</text>
						<text class="tep">{{item.phone}}</text>
					</view>
					<view class="addres-text" @click="handleCheck(item)">
						{{item.addressName}}{{item.detail}}
					</view>
					<view class="defaut-delete flex-sb">
						<view class="left" v-if="item.isDefault">
							<text class="label">默认</text>
						</view>
						<view class="right">
							<text class="set-defaut" v-if="!item.isDefault" @click="handleDefault(item)">设为默认</text>
							<text class="set-delete" @click="handleDelete(item.addressId)">删除</text>
						</view>
					</view>
				</view>
			</view>
			<!-- 空 -->
			<unt-empty type="address" style="margin-bottom: 150rpx;" v-else></unt-empty>
			<!-- 空 -->
			<!-- 底部按钮 -->
			<view class="addres-fixed">
				<view class="addres-fixed-content">
					<view class="add-addres-btn flex" @click="handleCreate()">
						<u-icon name="plus" color="#fff"></u-icon><text class="text">添加地址</text>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import * as Addres from "@/api/addres/index.js"
	import prop from "./props.js"
	export default {
		name: "unt-self-address",
		mixins: [prop],
		data() {
			return {
				show: false,
				address: {addressName:"请选择收获地址！"},
				list: []
			};
		},
		watch: {
			addressDefaut: {
				immediate: true,
				handler(val) {
					if (val!=null&&!uni.$unt.isEmptyObject(val)) {
						this.address = val;
					}
				}
			}
		},
		methods: {
			handleCreate() {
				this.$unts.navTo('pages_moon/addres/create/create')
			},
			async addressChangeModel() {
				await this.getAddres();
				this.show = true;
			},
			handleCheck(item) {
				this.address = item;
				this.$emit('update:addressDefaut', item);
				this.$emit('update:addressId', item.addressId);
				this.show = false;
			},
			handleDefault(item) {
				const data = Object.assign({}, item);
				data.isDefault = !data.isDefault
				Addres.add(data).then(res => {
					this.getAddres();
				})
			},
			getAddres() {
				const app=this;
				return new Promise(function(resolve, reject) {
					Addres.list({}).then(res => {
						app.list = res.data;
						resolve('true')
					}).catch(err=>resolve(err))
				})
				
			},
			handleDelete(id) {
				Addres.del({
					ids: id
				}).then(res => {
					this.getAddres();
				})
			},
			handleCreate() {
				this.$unts.navTo('pages_moon/addres/create/create')
			},
		}
	}
</script>
<style>
	.unt-selfaddress-contanier .u-popup__content__close {
		padding: 15rpx;
		background: #fff;
		border-radius: 50%;
		box-shadow: 1px 1px 1px #e9ebed, -1px -1px 1px #e9ebed;
	}
</style>
<style lang="scss" scoped>
	.unt-selfaddress-contanier {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		z-index: 9999;

		.user-check-address {
			width: 100%;
			display: flex;
			justify-content: flex-start;
			align-items: center;

			.addres-content {
				margin-left: 20rpx;

				.addres-detailed {
					font-size: 26rpx;
				}

				.contacts {
					font-size: 26rpx;
					color: #999;
				}
			}

			unt-icon {
				margin-left: auto;
			}
		}

		// 列表
		.addres-list {
			max-height: 90vh;
			min-height: 60vh;
			overflow-y: auto;
			padding-top: 20px;
			padding: 50rpx 15rpx 15rpx 15rpx;

			.item {
				padding: 25rpx;
				margin-bottom: 30rpx;
				font-size: 28rpx;

				.name-tep {
					font-weight: bold;

					.tep {
						margin-left: 25rpx;
					}
				}

				.addres-text {
					color: #999;
					margin: 15rpx 0px;
					padding-bottom: 15rpx;
					border-bottom: 1px solid #f0f0f4;
				}

				.defaut-delete {
					font-size: 24rpx;

					.left {
						.label {
							padding: 5rpx 10rpx;
							background: #01ab63;
							color: #fff;
							border-radius: 10rpx;
						}
					}

					.right {
						.set-delete {
							margin-left: 25rpx;
							color: #ff461f;
						}
					}

				}
			}
		}
		// 底部按钮
		.addres-fixed {
			position: fixed;
			bottom: var(--window-bottom);
			left: 0;
			right: 0;
			display: flex;
			z-index: 11;
			background: #fff;
			// 设置ios刘海屏底部横线安全区域
			padding-bottom: constant(safe-area-inset-bottom);
			padding-bottom: env(safe-area-inset-bottom);

			.addres-fixed-content {
				width: 100%;
				padding: 10rpx 50rpx;

				.add-addres-btn {
					background: linear-gradient(to right, #01ab63, #09BF09);
					text-align: center;
					color: #fff;
					padding: 20rpx 30rpx;
					border-radius: 200rpx;

					.text {
						margin-left: 10rpx;
					}
				}
			}

		}
	}
</style>